//
//  MoreCommentWidget.m
//  TodayComment
//
//  Created by 张吉阳 on 15/10/5.
//  Copyright (c) 2015年 xianrenmoshi. All rights reserved.
//

#import "MoreCommentWidget.h"
#import "MoreCommentCell.h"
#import "FxGetComment.h"
#import "DetailPage.h"
@implementation MoreCommentWidget
- (void)viewDidLoad
{
    [super viewDidLoad];
    _tableView.nightBackgroundColor = UIColorFromRGB(0x343434);
    _tableView.nightSeparatorColor = UIColorFromRGB(0x313131);
    _hasNextPage = NO;
    [_tableView addSubview:_toolView];
    self.cellIdentifier = @"MoreCommentCell";
    [self updateUI];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
   
}


- (void)updateUI
{
    
    [super updateUI];
    [self hideIndicator];
}

- (void)executeNextRealOp
{
    NSString *url = [NSString stringWithFormat:NextCommentURLFmt, _boardid, _newsid, ((unsigned long)self.listData.count - (unsigned long)(self.listData.count)%10)];
    
    NSDictionary *dictInfo = @{@"url":url,
                               };
    
    _operation = [[FxGetComment alloc] initWithDelegate:self opInfo:dictInfo];
    [_operation executeOp];
}


- (void)opSuccessEx:(NSMutableArray *)data opinfo:(NSDictionary *)dictInfo
{
    [_operation cancelOp];
    _operation = nil;
    if ([data count] <= 0) {
        [self hideIndicator];
        return;
    }
    [self.listData addObjectsFromArray:data];
    _hasNextPage = YES;
    [self updateUI];
    [self hideIndicator];
}




#pragma mark - tableView代理方法及数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  _hasNextPage?self.listData.count:self.listData.count;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     return 44;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = nil;
    BaseInfo *info = nil;
    
    if (indexPath.row < self.listData.count) {
        cellIdentifier = self.cellIdentifier;
        info = [self.listData objectAtIndex:indexPath.row];
    }
    else {
        //cellIdentifier = @"CommentMoreCell";
        //[self executeNextRealOp];
    }
    
    FxBaseCell *cell = (FxBaseCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        NSArray* Objects = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:tableView options:nil];
        cell = [Objects objectAtIndex:0];
        [cell initCell];
    }
    [cell setCellData:info];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailPage *page = [[DetailPage alloc] init];
    page.commentInfo = [self.listDataPro objectAtIndex:indexPath.row];
    UIViewController *owner =  self.owner;
    [owner.navigationController pushViewController:page animated:YES];
}



@end
